// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
});
const db = cloud.database()
const _ = db.command;

// 云函数入口函数
exports.main = async (event, context) => {
  // 对hot数据列表进行过滤，将状态为0的且已经过期的状态设置为status：3
  let newTime = new Date();
  newTime = newTime.setDate(newTime.getDate()-1);
  newTime = new Date(newTime);
  await db.collection('hot').where({
    status:0,
    startTime:_.lt(newTime)
  })
  .update({
    data:{
      status:3
    }
  })
  if(event.COUNT == 4){
    return db.collection('hot')
    .where({
      status:0
    })
    .orderBy('seeCount','desc')
    .limit(event.COUNT)
    .get()
  }
  return db.collection('hot')
  .where({
    status:0
  })
  .orderBy('seeCount','desc')
  .get()
}